<?php


namespace app\v2\controller;


use app\common\controller\Checking;
use function fast\array_except;
use think\Db;
use think\Exception;

class User extends Base
{

    /*
     * 资产 48
     */
    public function asset()
    {
        //我
        $user = Db::name('users')->where('user_id', '=', $this->userId)->find();
        //资产
        $asset = Db::name('users_asset')->where('user_id', '=', $this->userId)->find();
        //法币账户
        $legal = Db::name('users_legal')->where('user_id', '=', $this->userId)->find();
        //币币账户
        $coin = Db::name('coin')->where('user_id', '=', $this->userId)->find();
        //账户资产折合usdt
        $alllv = $asset['lv_frozen'] + $asset['lv_usable'] + $coin['lv'] + $legal['lv'];
        $allusdt = $coin['usdt'] + $legal['usdt'];
        //价格
        $usdt_cny = Db::table('market')->where('id', '=', 17)->value('now_price_cny');
        $lv_cny = 10;
        $data['user'] = $user;
        $data['asset'] = $asset;
        $data['legal'] = $legal;
        $data['coin'] = $coin;
        $data['allusdt'] = $allusdt;
        $data['usdt_cny'] = $usdt_cny;
        $data['usdt_cny'] = $lv_cny;
        Checking::response(200, '', $data);
    }

    /*
     * 我的团队 102
     */
    public function team()
    {
        $data['user'] = Db::name('users')->where('', '', $this->userId)->find();
        //我的直推
        $childs = Db::name('users')->where('pid', '=', $this->userId)->select();
        foreach ($childs as $k => $v) {
            $childs[$k]['pcount'] = Db::name('users')->where('pid', '=', $v['user_id'])->count();
        }
        $data['childs'] = $childs;
        $data['pcount'] = Db::name('users')->where('pid', '=', $this->userId)->count();
        //团队人数
//        $data['teamnum'] = Db::name('users_position')->where('pid', '=', $this->userId)->count();
        Checking::response(200, '', $data);
    }

    /*
     * 收益名细 103
     */
    public function profit()
    {
        $array = Db::name('users_account_log')
            ->where('user_id', '=', $this->userId)
            ->where('type', 'in', '9,10,11')
            ->select();
        $sum = Db::name('users_account_log')
            ->where('user_id', '=', $this->userId)
            ->where('type', 'in', '9,10,11')
            ->sum('money');
        foreach ($array as $k => $v) {
            $array[$k]['uid'] = Db::name('users')->where('id', '=', $v['source_id'])->value('uuid');
        }
        $data['array'] = $array;
        $data['sum'] = $sum;
        Checking::response(200, '', $data);
    }

    /*
     * 104 团队业绩
     */
    public function teamprofit()
    {
        $childs = Db::name('users_position')->where('pid', '=', $this->userId)->select();
        $array = [];
        $sum = 0;
        foreach ($childs as $k => $v) {
            $user['uid'] = Db::name('users')->where('id', '=', $v['user_id'])->value('uuid');
            $user['sum'] = Db::name('users_account_log')
                ->where('user_id', '=', $v['user_id'])
                ->where('type', 'in', '9,10,11')
                ->sum('money');
            $sum = $sum + $user['sum'];
            $array[] = $user;
        }
        $data['array'] = $array;
        $data['sum'] = $sum;
        Checking::response(200, '', $data);
    }

    /*
     * 激活码记录 110
     */
    public function active()
    {
        $data['num'] = Db::name('users_asset')->where('user_id', '=', $this->userId)->value('activate');
        $array = Db::name('activate')->where('user_id', '=', $this->userId)->select();
        foreach ($array as $k => $v) {
            $array['create_time'] = date('Y/m/d H:i:s', strtotime($v['create_time']));
        }
        $data['array'] = $array;
        Checking::response(200, '', $data);
    }

    /*
     * 激活码互转 111
     * user_id
     */
    public function giveactive()
    {
        $params = input('param.');
        if (!Checking::keyIsExistence('user_id', $params)) {
            Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
        }
        $num = Db::name('users_asset')->where('id', '=', $params['user_id'])->value('activate');
        if ($num < 1) {
            Checking::response(-201, '激活码数量不足');
        }
        if (!$num) {
            Checking::response(-201, '请输入正确的ID');
        }
        $res1 = Db::name('users_asset')->where('user_id', '=', $this->userId)->setDec('activate', 1);
        $res2 = Db::name('users_asset')->where('user_id', '=', $params['user_id'])->setInc('activate', 1);
        if (!$res1 || !$res2) {
            Checking::response(-201, '转让失败，请重试');
        }
        $give['number'] = 1;
        $give['after_number'] = Db::name('users_asset')->where('user_id', '=', $this->userId)->value('activate');
        $give['user_id'] = $this->userId;
        $give['source_id'] = $params['user_id'];
        $give['create_time'] = date('Y-m-d H:i:s');
        $give['type'] = 2;
        $res3 = Db::name('users_asset')->insert($give);
        $get['number'] = 1;
        $get['after_number'] = Db::name('users_asset')->where('user_id', '=', $params['user_id'])->value('activate');
        $get['user_id'] = $params['user_id'];
        $get['source_id'] = $this->userId;
        $get['create_time'] = date('Y-m-d H:i:s');
        $get['type'] = 3;
        $res4 = Db::name('users_asset')->insert($get);
        Checking::response(200, '转让成功');
    }

    /*
     * c1认证 53
     * truename
     * id_card
     */
    public function cone()
    {
        $data = Db::name('config')->where('name', '=', 'c1_num')->value('value');
        Checking::response(200, '', $data);
    }

    public function docone()
    {
        $flag = Db::name('users')->where('id','=',$this->userId)->value('is_attestation');
        if($flag > 0){
            Checking::response(-1, '您已经进行过C1认证，请勿重复认证');
        }
        $params = input('param.');
        if (!Checking::keyIsExistence('truename,id_card', $params)) {
            Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
        }
        $num = Db::name('config')->where('name', '=', 'c1_num')->value('value');
        $userasset = Db::name('users_asset')->where('user_id', '=', $this->userId)->find();
        if ($userasset['activate'] >= $num) {
            $res = Db::name('users_asset')->where('user_id', '=', $this->userId)->setDec('activate', $num);
            if ($res) {
                //添加记录
                $data['number'] = $num;
                $data['after_number'] = Db::name('users_asset')->where('user_id', '=', $this->userId)->value('activate');
                $data['user_id'] = $this->userId;
                $data['source_id'] = $this->userId;
                $data['create_time'] = $num;
                $data['type'] = 1;
                Db::name('activate')->insert($data);
                //更新users
                $res1 = Db::name('users')->where('id', '=', $this->userId)
                    ->update(array('truename' => $params['truename'], 'id_card' => $params['id_card'], 'is_attestation' => 1));
                if ($res1) {
                    Checking::response(200, 'c1认证成功');
                } else {
                    Checking::response(-201, 'c1认证失败，请重试');
                }
            } else {
                Checking::response(-201, '扣除激活币失败，请重试');
            }
        } else {
            Checking::response(-201, '激活币数量不足');
        }
    }

    /*
     * 55 c2认证
     */
    public function ctwo()
    {
        $data = Db::name('')->where('id', '=', $this->userId)
            ->field('truename,uuid,id_card')
            ->find();
        Checking::response(200, '', $data);
    }

    /*
     * c2认证 56
     * positive
     * national_emblem
     */
    public function doctwo()
    {
        $flag = Db::name('users')->where('id','=',$this->userId)->value('is_attestation');
        if($flag == 0){
            Checking::response(-1, '请先进行过C1认证');
        }else if($flag == 1){
            $ishave = Db::name('idcard')
                ->where('id','=',$this->userId)
                ->where('status','=',0)
                ->find();
            if($ishave){
                Checking::response(-1, '您已经提交过C2审核申请，请耐心等候');
            }
        }else if($flag == 2){
            Checking::response(-1, '您已经进行过C2认证，请勿重复认证');
        }
        $params = input('param.');
        if (!Checking::keyIsExistence('positive,national_emblem', $params)) {
            Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
        }
        $data['user_id'] = $this->userId;
        $data['positive'] = Tool::base64toimg($params['positive']);
        $data['national_emblem'] = Tool::base64toimg($params['national_emblem']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $res = Db::name('idcard')->insert($data);
        if ($res) {
            Checking::response(200, '上传证件照成功，请等待后台审核');
        } else {
            Checking::response(-1, '上传证件照失败，请重试');
        }
    }

    /*
     * 93支付账号页面
     */
    public function payment()
    {
        $data = Db::name('payment')
            ->where('id', '=', $this->userId)
            ->where('status', '=', 1)
            ->select();
        $yuming = 'http://'.$_SERVER['SERVER_NAME'];
        foreach ($data as $k=>$v){
            $data[$k]['details'] = $yuming.$v['details'];
        }
        Checking::response(200, '', $data);
    }

    /*
     * 甜添加或修改支付账号
     * action 1=添加 2=修改
     * type 1=支付宝,2=微信,3银行卡'
     *
     * paypass 交易密码
     * code 短信验证
     *
     * id
     *
     * account
     * image  base64格式的图片
     *
     * reaccount
     * bank_address
     * bank_branch
     * details
     */
    public function editpay()
    {
        $params = input('param.');
        $user = Db::name('users')->where('id','=',$this->userId)->find();
        //短信验证

        //密码验证
        if (!Checking::checkPassword($user['pay_password'], $params['paypass'])) {
            Checking::response(-5, Checking::MESSAGE['checkPassword']);
        }
        if (!Checking::keyIsExistence('action,type,paypass,code', $params)) {
            Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
        }
        if ($params['type'] != 3) { //非银行卡
            if (!Checking::keyIsExistence('account,image', $params)) {
                Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
            }
            $data['user_id'] = $this->userId;
            $data['type'] = $params['type'];
            $data['account'] = $params['account'];
            $data['create_time'] = $this->userId;
            $data['details'] = Tool::base64toimg($params['image']);
            if ($params['action'] == 1) {//添加
                $res = Db::name('payment')->insert($data);
                if ($res) {
                    Checking::response(200, '添加成功');
                } else {
                    Checking::response(-201, '添加失败');
                }
            } else {//修改
                if (!Checking::keyIsExistence('id', $params)) {
                    Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
                }
                $res = Db::name('payment')->where('id', '=', $params['id'])->update($data);
                if ($res) {
                    Checking::response(200, '修改成功');
                } else {
                    Checking::response(-201, '修改失败');
                }
            }
        } else {//银行卡
            if (!Tool::bankcard($params['account'])) {
                Checking::response(-201, '银行卡号格式不对');
            }
            if ($params['account'] != $params['reaccount']) {
                Checking::response(-201, '两次输入银行卡号不一样');
            }
            if (!Checking::keyIsExistence('account,bank_address,bank_branch', $params)) {
                Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
            }
            $data['user_id'] = $this->userId;
            $data['type'] = $params['type'];
            $data['account'] = $params['account'];
            $data['bank_address'] = $params['bank_address'];
            $data['bank_branch'] = $params['bank_branch'];
            $data['create_time'] = $this->userId;
            if ($params['action'] == 1) {//添加
                $res = Db::name('payment')->insert($data);
                if ($res) {
                    Checking::response(200, '添加成功');
                } else {
                    Checking::response(-201, '添加失败');
                }
            } else {//修改
                if (!Checking::keyIsExistence('id', $params)) {
                    Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
                }
                $res = Db::name('payment')->where('id', '=', $params['id'])->update($data);
                if ($res) {
                    Checking::response(200, '修改成功');
                } else {
                    Checking::response(-201, '修改失败');
                }
            }
        }
    }

    /*
     * 提币地址页面 100
     */
    public function withdraw()
    {
        $data = Db::name('withdraw_address')
            ->where('user_id', '=', $this->userId)
            ->where('status', '=', 2)
            ->select();
        Checking::response(200, '', $data);
    }

    /*
     * 提币地址绑定  99
     * type 1=绑定 2=删除
     * id 删除用
     * coin 1=USDT 2=LV
     * paypass 密码验证
     * code 短信验证
     * address
     * remarks
     */
    public function addwithdraw()
    {
        $params = input('param.');
        $user = Db::name('users')->where('id', '=', $this->userId)->find();
        //短信验证

        //密码验证
        if (!Checking::checkPassword($user['pay_password'], $params['password'])) {
            Checking::response(-5, Checking::MESSAGE['checkPassword']);
        }
        $res = false;
        if ($params['type'] == 1) {
            if (!Checking::keyIsExistence('address，coin,type', $params)) {
                Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
            }
            $save['user_id'] = $this->userId;
            $save['address'] = $params['address'];
            $save['remarks'] = $params['remarks'];;
            $save['type'] = $params['coin'];
            $save['status'] = 1;
            $save['create_time'] = date('Y-m-d h:i:s');
            $res = Db::name('withdraw_address')->insert($save);
        } else {
            if (!Checking::keyIsExistence('id', $params)) {
                Checking::response(-1, Checking::MESSAGE['keyIsExistence']);
            }
            $res = Db::name('withdraw_address')->where('id', '=', $params['id'])
                ->update(['status' => 2, 'update_time' => date('Y-m-d H:i:s')]);
        }
        if (!$res) {
            Checking::response(-201, '绑定提币地址失败，请重试');
        }
        Checking::response(200, '绑定提币地址成功');
    }
}